package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.util.PicChooseUtils;
import com.xmb.wechat.widget.TitleLayout;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatContactAddActivity extends BaseActivity {
    public static final int IMAGE_PICKER = 101;

    @BindView(R2.id.title_layout)
    TitleLayout  mTitleLayout;
    @BindView(R2.id.iv_avatar)
    ImageView    ivAvatar;
    @BindView(R2.id.et_name)
    EditText     etName;
    @BindView(R2.id.ll_no_dis)
    LinearLayout mLLNoDis;
    @BindView(R2.id.switch_no_dis)
    Switch       mSwitchNoDis;
    @BindView(R2.id.ll_strong_remind)
    LinearLayout mLLStrongRemind;
    @BindView(R2.id.switch_remind)
    Switch       mSwitchRemind;

    WechatContactBean vo;
    String            avatarPath;
    boolean           mNeedFeedBack;

    public WechatContactAddActivity() {
        super(R.layout.wechat_activity_add_contact);
    }

    //编辑
    public static void start4Edit(Activity act, WechatContactBean vo) {
        Intent it = new Intent(act, WechatContactAddActivity.class);
        it.putExtra("WechatContactBean", vo);
        act.startActivity(it);
    }

    //编辑(带返回)
    public static void start4Edit4Result(Activity act, WechatContactBean vo, int requestCode) {
        Intent intent = new Intent(act, WechatContactAddActivity.class);
        intent.putExtra("WechatContactBean", vo);
        intent.putExtra("feedBack", true);
        act.startActivityForResult(intent, requestCode);
    }

    //新增（带返回）
    public static void startForResult(Activity ctx, boolean isNeedFeedBack, int requestCode) {
        Intent intent = new Intent(ctx, WechatContactAddActivity.class);
        intent.putExtra("feedBack", isNeedFeedBack);
        ctx.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mNeedFeedBack = getIntent().getBooleanExtra("feedBack", false);
        vo = (WechatContactBean) getIntent().getSerializableExtra("WechatContactBean");
        //不为空就是编辑该联系人：
        if (vo != null) {//编辑
            etName.setText(vo.getName());
            avatarPath = vo.getAvatar();
            mTitleLayout.setTitle("编辑联系人");
            if (!mNeedFeedBack) {//从联系人列表进来的才显示免打扰选项
                mLLNoDis.setVisibility(View.VISIBLE);
                mLLStrongRemind.setVisibility(View.VISIBLE);
                mSwitchNoDis.setChecked(vo.isMute());
                mSwitchRemind.setChecked(vo.isStrongRemind());
            }
            WechatContactBean.setupAvatarIntoImageView(vo, ivAvatar, 12);
        } else {//新增
            vo = new WechatContactBean();
        }
        mTitleLayout.setRightClick(v -> onClickSave());
        mSwitchNoDis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (vo != null)
                    vo.setMute(isChecked);
            }
        });
        mSwitchRemind.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (vo != null)
                    vo.setStrongRemind(isChecked);
            }
        });
    }

    @OnClick({R2.id.btn_pick_avatar, R2.id.btn_save})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.btn_pick_avatar) {
            PicChooseUtils.chooseOnePicWithCut(this, 1, 1, Pic -> {
                vo.setAvatar(Pic[0]);
                vo.setAvatarSrc(Constant.SourceType.AVATAR_SRC_SDCARD);
                ivAvatar.setImageBitmap(BitmapFactory.decodeFile(Pic[0]));
            });
        } else if (view.getId() == R.id.btn_save) {
            onClickSave();
        }
    }

    private void onClickSave() {
        final String name = etName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showLong("昵称不能为空！");
            return;
        }
        if (TextUtils.isEmpty(vo.getAvatar())) {
            new AlertDialog.Builder(this).setTitle("提示").setMessage("您未选择联系人头像，是否使用默认头像？").setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            vo.setAvatar(R.drawable.ic_wx_default_avatar + "");
                            vo.setAvatarSrc(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE);
                            save2DB(name);
                        }
                    }).setNegativeButton("取消", null).create().show();
            return;
        }
        save2DB(name);
    }

    private void save2DB(String name) {
        vo.setName(name);
        if (mNeedFeedBack) {
            WechatStartApplication.getBoxStore(this).boxFor(WechatContactBean.class).put(vo);
            ToastUtils.showLong("保存成功！");
            Intent intent = new Intent();
            intent.putExtra("result", vo);
            setResult(RESULT_OK, intent);
        } else {
            WechatStartApplication.getBoxStore(this).boxFor(WechatContactBean.class).put(vo);
            ToastUtils.showLong("保存成功！");
        }
        finish();
    }
}
